package com.efast.dmom.aps.datahandlers;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.PlanOrder;
import com.efast.dmom.aps.bean.PlanOrderAdjust;
import com.efast.dmom.aps.bean.PlanOrderAdjustQueryCondition;
import com.efast.dmom.aps.dao.PlanOrderDao;
import com.efast.dmom.aps.entity.PlanOrderEntity;
import com.efast.dmom.aps.util.CommonConstants;
import com.efast.dmom.aps.util.TimeUtil;

/**
 * 
 * @author wj
 *
 */
@Component
public class PlanOrderDataHandler{

	@Autowired
	private PlanOrderDao planOrderDao;
	
	public List<PlanOrder> getUnbroadcastPlanOrders(long prodLineId) {
		
		List<PlanOrder> orderList = new ArrayList<PlanOrder>();
		
		List<PlanOrderEntity> entityList = planOrderDao.getUnbroadcastPlanOrders(prodLineId);
		
		if(entityList != null){
			
			for(PlanOrderEntity entity : entityList){
				
				PlanOrder order = new PlanOrder();
				order.setInnerData(entity);
				
				orderList.add(order);
			}
		}
		return orderList;
	}
	
	 void setPlantShopId(PlanOrderEntity orderEntity){
		 
		 PlanOrderEntity entity = planOrderDao.setPlantShopId(orderEntity);
		 
		 if(entity != null){
			 
			 orderEntity.setPlantId(entity.getPlantId());
			 orderEntity.setShopId(entity.getShopId());
		 }
	 }
	
	public void updatePlanOrder(List<PlanOrder> orderList) {
		
		List<PlanOrderEntity> entityList = new ArrayList<PlanOrderEntity>();
		
		if(orderList != null && orderList.size() >0){
			
			for(PlanOrder order : orderList){
				
				PlanOrderEntity orderEntity = order.getInnerData();
				orderEntity.setOrderStatus(CommonConstants.APS_PLAN_ORDER_STATUS_SCHEDULED);
				entityList.add(orderEntity);
			}
		}
		
		if(entityList.size() > 0){
			planOrderDao.updatePlanOrder(entityList);
		}
		
	}

	public void save(PlanOrder obj) {
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setCreationPid(dmomIdDao.getDmomPid());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		
		planOrderDao.save(obj.getInnerData());
	}

	public void update(PlanOrder obj) {
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		
		planOrderDao.update(obj.getInnerData());
	}

	public void delete(PlanOrder obj) {
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		planOrderDao.delete(obj.getInnerData());
	}

	public PlanOrder getById(long id) {
		// TODO Auto-generated method stub
		PlanOrder planOrder = new PlanOrder();
		planOrder.setInnerData(planOrderDao.getById(id));
		
		return planOrder;
	}

	public PlanOrder getByName(String name) {
		// TODO Auto-generated method stub
		PlanOrder order = new PlanOrder();
		order.setInnerData(planOrderDao.getByName(name));
		
		return order;
	}
	
	public PlanOrder getByOrderNo(String orderNo) {
		// TODO Auto-generated method stub
		PlanOrder order = new PlanOrder();
		PlanOrderEntity byOrderNo = planOrderDao.getByOrderNo(orderNo);
		if(byOrderNo!=null ) {
			order.setInnerData(byOrderNo);
		}
		return order;
	}
	
	//Obtain unissued and frozen scheduled work orders
	public List<PlanOrder> getUnbroadcastAndUnFreezePlanOrders() {
		List<PlanOrderEntity> entityList = planOrderDao.getUnbroadcastAndUnFreezePlanOrders();
		if (entityList == null || entityList.size() == 0) {
			return null;
		}
		List<PlanOrder> planOrderList = new ArrayList<PlanOrder>();
		for(PlanOrderEntity innerData:entityList) {
			PlanOrder planOrder = new PlanOrder();
			planOrder.setInnerData(innerData);
			planOrderList.add(planOrder);
		}
		return planOrderList;
	}

	public List<PlanOrderAdjust> getDataByPlanOrderAdjust(PlanOrderAdjustQueryCondition planOrderAdjustQueryCondition) {
		List<PlanOrderAdjust> list = planOrderDao.getDataByPlanOrderAdjust(planOrderAdjustQueryCondition);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
	
	public List<PlanOrder> getByLineAndTime(long lineId,LocalDateTime startTime,LocalDateTime endTime) {
		List<PlanOrder> planOrderList = new ArrayList<PlanOrder>();
		
		List<PlanOrderEntity> entityList = planOrderDao.getByLineAndTime( lineId, startTime, endTime);

	
		for(PlanOrderEntity innerData:entityList) {
			PlanOrder planOrder = new PlanOrder();
			planOrder.setInnerData(innerData);
			planOrderList.add(planOrder);
		}
		
		
		return planOrderList;
	}
	public PlanOrder getMiddlePlanOrderByLineAndTime(long lineId,LocalDateTime time) {
		
		PlanOrderEntity innerData = planOrderDao.getMiddlePlanOrderByLineAndTime( lineId, time);
		if(innerData!=null) {
			PlanOrder planOrder = new PlanOrder();
			planOrder.setInnerData(innerData);
			return planOrder;
		}else {
			return null;
		}
	}
}

